package com.hilinos.base.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hilinos.base.api.vo.AccountAddVo;
import com.hilinos.base.api.vo.UserVo;
import com.hilinos.base.app.UserApp;
import com.hilinos.base.common.config.ServerConfig;
import com.hilinos.base.user.entity.pojo.UserInfoPoJo;
import com.hilinos.base.user.entity.vo.UserLoginVo;
import com.hilinos.common.common.ReturnCode;
import com.hilinos.common.common.handler.NoToken;
import com.hilinos.common.entity.BaseQo;
import com.hilinos.common.util.AssertUtils;
import com.hilinos.common.util.ServletUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api("用户接口")
@RestController()
@RequestMapping("user")
public class UserController {

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private UserApp iUserApp;

    @ApiOperation("创建用户")
    @PostMapping("createUser")
    public void addUserAndAccount(@RequestParam UserVo userVo) {
        userVo.checkCreateParams();
        iUserApp.addUser(userVo);
    }

    @ApiOperation("创建用户并创建账户")
    @PostMapping("createUserAndAccount")
    public void addUserAndAccount(@RequestParam UserVo userVo, @RequestParam AccountAddVo accountVo) {
        userVo.setAppid(ServletUtils.getAppid());
        accountVo.setAppid(userVo.getAppid());
        userVo.checkCreateParams();
        accountVo.checkCreateParams();
        iUserApp.addUserAndAccount(userVo, accountVo);
    }

    @ApiOperation("修改用户信息")
    @PostMapping("edit")
    public void editUser(@RequestBody UserVo userVo) {
        userVo.checkCreateParams();
        iUserApp.editUser(userVo);
    }

    @NoToken(needToken = true, onlySuperAdmin = true)
    @ApiOperation("查询用户列表")
    @PostMapping("pageList")
    public IPage<UserInfoPoJo> userPageList(@RequestBody BaseQo<UserInfoPoJo, UserVo, UserVo> userQo) {
        userQo.checkParams();
        return iUserApp.userPageList(userQo);
    }

    @ApiOperation("用户绑定账号")
    @PostMapping("bindAccount")
    public void bindAccount(@RequestBody AccountAddVo accountVo) {
        accountVo.checkBindParams();
        iUserApp.bindAccount(accountVo);
    }

    @NoToken
    @PostMapping("login")
    public UserLoginVo login(String appid, String userType, String accountId, String accountPassword,
            String accountType, String orgId) {
        AssertUtils.isNotEmpty(appid, ReturnCode.NORMAL_PARAM_LOST_ERROR.respMsg("appid不能为空"));
        AssertUtils.isNotEmpty(userType, ReturnCode.NORMAL_PARAM_LOST_ERROR.respMsg("用户类型不能为空"));
        AssertUtils.isNotEmpty(accountId, ReturnCode.NORMAL_PARAM_LOST_ERROR.respMsg("账号不能为空"));
        AssertUtils.isNotEmpty(accountPassword, ReturnCode.NORMAL_PARAM_LOST_ERROR.respMsg("密码不能为空"));
        return iUserApp.login(appid, userType, accountId, accountPassword,
                ObjectUtils.isEmpty(accountType) ? serverConfig.getDefaultAccountType() : accountType, orgId);
    }

    @PostMapping("check/token")
    public UserLoginVo checkToken(@RequestHeader String token) {
        AssertUtils.isNotEmpty(token, ReturnCode.NORMAL_PARAM_LOST_ERROR.respMsg("token不能为空"));
        return iUserApp.checkToken(token);
    }

    @PostMapping("remove/token")
    public void removeToken(String token) {
        AssertUtils.isNotEmpty(token, ReturnCode.NORMAL_PARAM_LOST_ERROR.respMsg("token不能为空"));
        iUserApp.removeToken(token);
    }

}
